package com.housediyadmin.view.components
{   	
	import com.housediyadmin.model.ColorModel;
	import com.housediyadmin.model.GoodsModel;
	import com.housediyadmin.model.PicModel;
	import com.housediyadmin.model.SizeModel;
	import com.housediyadmin.vo.Goods;
	import com.housediyadmin.vo.Picture;
	import com.housediyadmin.vo.Size;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.FileReference;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.Image;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.events.IndexChangeEvent;
	
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/3/05
	 */ 
	public class PicShowCompMediator extends Mediator
	{
		[Inject]
		public var view:PicShowComp;
		
		[Inject]
		public var picModel:PicModel;
		
		[Inject]
		public var sizeModel:SizeModel;
		
		[Inject]
		public var colorModel:ColorModel;
		
		[Inject]
		public var goodsModel:GoodsModel;
		
		[Bindable]
		public var currentPic:FileReference;
		
		[Bindable]
		public var index:int;
		
		[Bindable]
		public var setting:ArrayCollection = new ArrayCollection([{"lable":"默认"}, {"lable":"其他"}]);
		/*public var pic:Picture = new Picture();*/
		public var sizeID:int;
		public var colorID:int;
		override public function onRegister():void
		{
			index = 0;
			currentPic = new FileReference();
			currentPic = picModel.selectedFiles[index];
			BindingUtils.bindProperty(view.picList, "dataProvider", picModel, "addPicArray");
			BindingUtils.bindProperty(view.picNameTX, "text", currentPic, "name");
			view.img.source = "http://localhost/housediyadmin/upload/" + currentPic.name;
			/*view.nextPic.addEventListener(MouseEvent.CLICK, nextPicHandler);
			view.previewPic.addEventListener(MouseEvent.CLICK, previewPicHandler);*/
			/*view.nextPic.addEventListener(MouseEvent.CLICK, nextPicHandler);*/
            view.picList.addEventListener(IndexChangeEvent.CHANGE, showPicHandler);
			view.setDoneBtn.addEventListener(MouseEvent.CLICK, setDoneHandler);
			switch(view.currentState)
			{
				case "base":
				view.setPic.dataProvider = setting;
                break;				
				case "append":
				BindingUtils.bindProperty(view.selectSize, "dataProvider", sizeModel, "sizeArray");
				BindingUtils.bindProperty(view.selectColor, "dataProvider", colorModel, "colorArray");
				view.selectColor.addEventListener(IndexChangeEvent.CHANGE, selectColorHandler);
				view.selectSize.addEventListener(IndexChangeEvent.CHANGE, selectSizeHandler);
				break;
			}
		}
		
		/*private function nextPicHandler(event:MouseEvent):void
		{		
			if(picModel.addPicArray.length != picModel.selectedFiles.length)
			{
				if(index < picModel.selectedFiles.length-1)
				{
					index++;
				}
				else
				{
					index = 0;
				}			
				currentPic= picModel.selectedFiles[index];
				view.picNameTX.text = currentPic.name;
				view.img.source = "http://localhost/housediyadmin/upload/" + currentPic.name;				
			}
			else
			{
				view.nextPic.visible = false;
				view.previewPic.visible = false;
			}
			
		}*/
		
	/*	private function previewPicHandler(event:MouseEvent):void
		{
			if(index != 0)
			{
				index --;
			}
			else
			{
				index = picModel.selectedFiles.length -1;
			}
			currentPic = picModel.selectedFiles[index];
			view.picNameTX.text = currentPic.name;
			view.img.source = "http://localhost/housediyadmin/upload/" + currentPic.name;			
		}*/
		
		private function showPicHandler(event:IndexChangeEvent):void
		{
			view.setDoneBtn.enabled = true;
			var picture:Picture = view.picList.selectedItem as Picture;
		    view.img.source = "http://localhost/housediyadmin/upload/" + picture.pictureSrc;
			view.picNameTX.text = picture.pictureName;
			if(picture.isDefault == true)
			{
				view.setPic.selectedIndex = 0;
			}
			else
			{
				view.setPic.selectedIndex = 1;
			}
		}
		
		private function setDoneHandler(event:Event):void
		{
			if(index < picModel.selectedFiles.length-1)
			{
				index++;
			}
			else
			{
				index = 0;
			}			
			currentPic= picModel.selectedFiles[index];
			view.picNameTX.text = currentPic.name;
			view.img.source = "http://localhost/housediyadmin/upload/" + currentPic.name;	
			
			var pic:Picture = new Picture();
			pic.modelID = 0;			
			pic.goodsID = goodsModel.currentGoods.goodsID;
			pic.picCreateDate = new Date();
			pic.picEditDate = new Date();
			pic.pictureSrc = currentPic.name;
			pic.pictureName = currentPic.name;
			switch(view.currentState)
			{
				case "base":
					pic.sizeID = 0;
					pic.colorID = 0;
					pic.isMap = false;
					if(view.setPic.selectedIndex == 0)
					{
						pic.isDefault = true;
					}
					else
					{
						pic.isDefault = false;
					}
					view.setPic.selectedIndex = 0;
					for each (var tempPic:Picture in picModel.addPicArray)
				{
					if(tempPic.pictureName == pic.pictureName)
					{
						Alert.show("已为此图片设置属性");
						//picModel.addPicArray.setItemAt(pic,2) 用这个方法设置
					}
					
					
				}
				    break;
				
				case "append":
					pic.sizeID = sizeID;
					if(view.isDragCb.selected)
					{
						pic.isMap = true;
					}
					if(view.isShowCb.selected)
					{
						pic.isMap = true;
					}
					if(view.isShowCb.selected)
					{
						pic.isMap = true;
					}
				    break;
			}	
			
			picModel.addPicArray.addItem(pic);
			if(picModel.addPicArray.length == picModel.selectedFiles.length)
			{
				view.setDoneBtn.enabled = false;
			}
		}
		
		private function selectColorHandler(event:IndexChangeEvent):void
		{
			
		}
		
		private function selectSizeHandler(event:IndexChangeEvent):void
		{
			sizeID = (view.selectSize.selectedItem as Size).sizeID;
		}
			
	}
}